﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Web.usercenter.ViewModels;
using MiniWeb.Web.Controllers.Web.web.ViewModels;
using WFramework;
using WFramework.Common;
using WFramework.Utility;

namespace MiniWeb.Web.Controllers.Web.UserCenter
{
    public class UserCenterController : BaseController
    {
        public readonly AccountsInfoTask _AccountsInfoTask = new AccountsInfoTask();
        public readonly OrderAddressTask _OrderAddressTask = new OrderAddressTask();
        public readonly OrdersTask _OrdersTask = new OrdersTask();
        public readonly OrdersDetailTask _OrdersDetailTask = new OrdersDetailTask();
        public ActionResult Index()
        {
            var model = _AccountsInfoTask.GetByAccount(WebFormsAuthService.GetCurrentAccount());

            return View(model);
        }


        public ActionResult Orders()
        {
            var account = WebFormsAuthService.GetCurrentAccount();
            var orderlist = _OrdersTask.GetAll(100, string.Format("Account= '{0}'", account), "");
            List<UserOrderListDto> list = new List<UserOrderListDto>();
            foreach (var item in orderlist)
            {
                list.Add(new UserOrderListDto
                {
                    order = item,
                    detaillist = _OrdersDetailTask.GetAll(100, string.Format("OrderCode='{0}'", item.Code), "")
                });
            }
            return View();
        }

        [HttpPost]
        public ActionResult order()
        {
            return View();
        }

        public ActionResult orderdetail(string code)
        {
            var ordermodel = _OrdersTask.GetByCode(code);
            var orderlistmodel = _OrdersDetailTask.GetListByCode(code);
            if (orderlistmodel == null)
            {
                return new EmptyResult();
            }
            ViewData["orderlistmodel"] = orderlistmodel;
            var orderaddressmodel = _OrderAddressTask.GetByAccount(ordermodel.Account);
            if (orderlistmodel == null)
            {
                return AlertMsg("请填写收获地址信息", @Url.Action("address", "usercenter"));
            }
            ViewData["orderaddressmodel"] = orderaddressmodel;
            return View(ordermodel);
        }


        //个人信息
        public ActionResult Info()
        {
            return View();
        }

        public ActionResult ChangePwd()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ChangePwd(string pwd, string cpwd, string cpw1)
        {
            if (string.IsNullOrEmpty(cpwd) || string.IsNullOrEmpty(pwd))
            {
                return AlertMsg("密码不能为空！", Request.UrlReferrer.ToString());
            }
            if (cpwd != cpw1)
            {
                return AlertMsg("密码不一致！", Request.UrlReferrer.ToString());
            }
            var model = _AccountsInfoTask.GetByAccount(WebFormsAuthService.GetCurrentAccount());
            if (model == null)
            {
                return new EmptyResult();
            }
            if (model.Password != CryptTools.HashPassword(pwd))
                return AlertMsg("账号或密码不正确", Request.UrlReferrer.PathAndQuery);
            model.Password = CryptTools.HashPassword(cpwd);
            _AccountsInfoTask.Update(model);
            return AlertMsg("修改成功！", "/usercenter/changepwd");

        }

        public ActionResult Address()
        {
            var model = new WebSaveOrderAddress();
            var myaddress = _OrderAddressTask.GetByAccount(WebFormsAuthService.GetCurrentAccount());
            if (myaddress != null)
            {
                var citylist = myaddress.City.Split('+');
                model.RealName = myaddress.RealName;
                model.Sheng = myaddress.Sheng;
                model.City = citylist.Count()>1? myaddress.City.Split('+')[0]:"";
                model.ICity = citylist.Count() > 1 ? myaddress.City.Split('+')[1] : "";
                model.Address = myaddress.Address;
                model.Phone = myaddress.Phone;
                model.ID = myaddress.ID;
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult SaveAddress(WebSaveOrderAddress savemodel)
        {

            if (ModelState.IsValid)
            {

                if (savemodel.ID == null)
                {
                    var model = new OrderAddress
                                    {
                                        Account = WebFormsAuthService.GetCurrentAccount(),
                                        RealName = savemodel.RealName,
                                        Phone = savemodel.Phone,
                                        Address = savemodel.Address,
                                        Sheng = savemodel.Sheng,
                                        City = savemodel.City + "+" + savemodel.ICity,
                                        IsDefault = false,
                                        CreateDate = DateTime.Now,
                                    };
                    _OrderAddressTask.Add(model);
                }
                else
                {
                    var model = _OrderAddressTask.GetById((int)savemodel.ID);

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);


                    model.RealName = savemodel.RealName;
                    model.Phone = savemodel.Phone;
                    model.Address = savemodel.Address;
                    model.Sheng = savemodel.Sheng;
                    model.City = savemodel.City + "+" + savemodel.ICity;
                    model.IsDefault = savemodel.IsDefault;


                    _OrderAddressTask.Update(model);

                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);

            }

            return AlertMsg("保存失败", HttpContext.Request.UrlReferrer.PathAndQuery);

        }
      
    }
}
